#include <iostream>
#include <memory>
using namespace std;
using std::cout;
using std::endl;
// 策略模式
class Strategy
{
public:
    virtual void execute() = 0;
    virtual ~Strategy() {}
};
class ConcreateStrategyA : public Strategy
{
public:
    virtual void execute() override
    {
        cout << "ConcreateStrategyA :: execute()" << endl;
    }
};
class ConcreateStrategyB : public Strategy
{
public:
    virtual void execute() override
    {
        cout << "ConcreateStrategyB :: execute()" << endl;
    }
};
class Context
{
public:
    Context(Strategy *pstra)
        : _pstra(pstra)
    {
    }
    void setStrategy(Strategy *ps){
        _pstra = ps;
    }
    void executeStragety(){
        if(_pstra){
            _pstra->execute();
        }
    }
    ~Context(){}

private:
    Strategy *_pstra;
};
void test()
{
    //Strategy * pa -> ConcreateStrategyA 基类指针指向派生类对象多态
    unique_ptr<Strategy> pa(new ConcreateStrategyA());
    unique_ptr<Strategy> pb(new ConcreateStrategyB());
    Context ctx(pa.get());
    ctx.executeStragety();

    //更换策略
    ctx.setStrategy(pb.get());
    ctx.executeStragety();
}
int main()
{
    test();
    return 0;
}
